package disk;

import java.util.Arrays;

/**
 * 文件类
 * 
 * @author vincent
 * 
 */
public class File extends DirectoryEntry {
	/**
	 * 文件内容
	 */
	protected byte[] content;

	/**
	 * 构造函数
	 */
	public File(byte[] name) {
		// TODO 自动生成的构造函数存根
		this(name, new byte[2], FileAttribute.ISCOMMONFILE, (byte) -1, (byte) -1);
	}

	public File(byte[] name, byte[] typeName) {
		this(name, typeName, FileAttribute.ISCOMMONFILE, (byte) -1, (byte) -1);
		this.content = new byte[0];
	}

	public File(byte[] name, byte[] typeName, byte[] content) {
		this(name, typeName, FileAttribute.ISCOMMONFILE, (byte) -1, (byte) -1);
		this.content = content;
	}

	/**
	 * 构造函数
	 * 
	 * @param name
	 * @param typeName
	 * @param attribute
	 * @param blockIndex
	 * @param length
	 */
	public File(byte[] name, byte[] typeName, byte attribute, byte blockIndex,
			byte length) {
		super(name, typeName, attribute, blockIndex, length);
	}

	/**
	 * 设置文件内容
	 * 
	 * @param content
	 */
	public void setContent(byte[] content) {
		this.content = content;
	}

	/**
	 * 获取文件内容
	 * 
	 * @return
	 */
	public byte[] getContent() {
		return content;
	}

	/**
	 * 获取文件内容的长度(字节)
	 * 
	 * @return
	 */
	public int getContentLength() {
		return content.length;
	}

	/**
	 * 获取完整文件名(文件名+类型名)
	 * 
	 * @return
	 */
	public String getCompleteName() {
		int actNameLength = 0;
		for (int i = 0; i < name.length; i++) {
			if (name[i] == 0) {
				break;
			}
			actNameLength++;
		}
		int actTypeNameLength = 0;
		for (int i = 0; i < typeName.length; i++) {
			if (typeName[i] == 0) {
				break;
			}
			actTypeNameLength++;
		}
		return new String(Arrays.copyOf(name, actNameLength)) + "."
				+ new String(Arrays.copyOf(typeName, actTypeNameLength));
	}
}
